package com.lire.shop.data;

import com.lire.shop.dao.ProductRepository;
import com.lire.shop.dao.UserRepository;
import com.lire.shop.dao.WalletRepository;
import com.lire.shop.entity.Product;
import com.lire.shop.entity.User;
import com.lire.shop.entity.Wallet;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigDecimal;
import java.util.ArrayList;

@Configuration
public class DataConfig {

    @Bean
    public InitializingBean initializingBeanUser(UserRepository userRepository) {
        return () -> {
            ArrayList<User> users = new ArrayList<User>();
            User user;
            for (int i = 1; i <= 8; i++) {
                user = new User();
                user.setUid((long) i);
                user.setUsername("lire-" + i);
                user.setPassword("password-" + i);
                user.setWid((long) i);
                users.add(user);
            }
            userRepository.saveAll(users);
        };
    }


    @Bean
    public InitializingBean initializingBeanWallet(WalletRepository walletRepository) {
        return () -> {
            ArrayList<Wallet> wallets = new ArrayList<>();
            Wallet wallet ;
            for (int i = 1; i <= 8; i++) {
                wallet = new Wallet();
                wallet.setWid((long) i);
                wallet.setBalances(new BigDecimal(i + "000.00"));
                wallets.add(wallet);
            }
            walletRepository.saveAll(wallets);
        };
    }

    @Bean
    public InitializingBean initializingBeanProduct(ProductRepository productRepository) {
        return () -> {
            ArrayList<Product> products = new ArrayList<>();
            Product product;
            for (int i = 1; i <= 8; i++) {
                product = new Product();
                product.setPid((long) i);
                product.setName("product-" + i);
                product.setPrice(i + "00.00");
                product.setStock(i + 1);
                products.add(product);
            }
            productRepository.saveAll(products);
        };
    }
}
